using System;
using System.Collections.Generic;
using System.Text;

namespace ComputaGuru.SpEAd
{
    /// <summary>
    /// Definition of the Geometry and visibility of a Label on a 
    /// </summary>
    internal class DiagramLinkLabel
    {
        private string labelName = String.Empty;
        private string cx = String.Empty;
        private string cy = String.Empty;
        private string ox = String.Empty;
        private string oy = String.Empty;
        private string hdn = String.Empty;
        private string bld = String.Empty;
        private string ita = String.Empty;
        private string und = String.Empty;
        private string clr = String.Empty;
        private string aln = String.Empty;
        private string dir = String.Empty;
        private string rot = String.Empty;

        //properties for fields in a label definition
        public string LabelName
        {
            get { return labelName; }
            set { labelName = value; }
        }

        internal string CX
        {
            get { return cx; }
            set { cx = value; }
        }

        internal string CY
        {
            get { return cy; }
            set { cy = value; }
        }

        internal string OX
        {
            get { return ox; }
            set { ox = value; }
        }

        internal string OY
        {
            get { return oy; }
            set { oy = value; }
        }

        internal string HDN
        {
            get { return hdn; }
            set { hdn = value; }
        }

        internal string BLD
        {
            get { return bld; }
            set { bld = value; }
        }

        internal string ITA
        {
            get { return ita; }
            set { ita = value; }
        }

        internal string UND
        {
            get { return und; }
            set { und = value; }
        }

        internal string CLR
        {
            get { return clr; }
            set { clr = value; }
        }

        internal string ALN
        {
            get { return aln; }
            set { aln = value; }
        }

        internal string DIR
        {
            get { return dir; }
            set { dir = value; }
        }

        internal string ROT
        {
            get { return rot; }
            set { rot = value; }
        }

        /// <summary>
        /// Contructor to parse a string and build the object
        /// </summary>
        /// <param name="labeldefinition"></param>
        internal DiagramLinkLabel(string labeldefinition)
        {
            //a label definition begins with a symbol that indicates which label it is
            //the remainder of the definition is assigned to the name with an =
            //the fields following this are delimited with a colon
            //if the label was not actually defined the definition will
            //be nothing more than the name followed by =

            if (labeldefinition.EndsWith("="))
            {
                this.LabelName = labeldefinition.Substring(0, (labeldefinition.Length - 1));
            }
            else
            {

                //split the difinition into fields
                //the first one will include the label identifier
                string[] labelFields = labeldefinition.Split(':');

                //iterate through the fields and populate properties
                //of this instance with values
                for (int i = 0; i < labelFields.Length; i++)
                {
                    if (i == 0)
                    {
                        //capture and strip the label identifier/name from
                        //the first field
                        int nameDelimiterIndex = labelFields[i].IndexOf('=');
                        this.LabelName = labelFields[i].Substring(0, nameDelimiterIndex);
                        labelFields[i] = labelFields[i].Substring(nameDelimiterIndex + 1);
                    }

                    string[] fieldParts = labelFields[i].Split('=');
                    switch (fieldParts[0])
                    {
                        case "CX":
                            this.CX = fieldParts[1];
                            break;
                        case "CY":
                            this.CY = fieldParts[1];
                            break;
                        case "OX":
                            this.OX = fieldParts[1];
                            break;
                        case "OY":
                            this.OY = fieldParts[1];
                            break;
                        case "HDN":
                            this.HDN = fieldParts[1];
                            break;
                        case "BLD":
                            this.BLD = fieldParts[1];
                            break;
                        case "ITA":
                            this.ITA = fieldParts[1];
                            break;
                        case "UND":
                            this.UND = fieldParts[1];
                            break;
                        case "CLR":
                            this.CLR = fieldParts[1];
                            break;
                        case "ALN":
                            this.ALN = fieldParts[1];
                            break;
                        case "DIR":
                            this.DIR = fieldParts[1];
                            break;
                        case "ROT":
                            this.ROT = fieldParts[1];
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// Overridden to output the label definition required as part of the Geometry property
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder returnString = new StringBuilder(this.LabelName + "=");

            //generally it appears that if a label is defined
            //all fields will have some kind of value even if it is "0"
            //so for now proceeding on this basis
            if (this.CX != String.Empty)
            {
                returnString.Append("CX=" + this.CX + ":");
                returnString.Append("CY=" + this.CY + ":");
                returnString.Append("OX=" + this.OX + ":");
                returnString.Append("OY=" + this.OY + ":");
                returnString.Append("HDN=" + this.HDN + ":");
                returnString.Append("BLD=" + this.BLD + ":");
                returnString.Append("ITA=" + this.ITA + ":");
                returnString.Append("UND=" + this.UND + ":");
                returnString.Append("CLR=" + this.CLR + ":");
                returnString.Append("ALN=" + this.ALN + ":");
                returnString.Append("DIR=" + this.DIR + ":");
                returnString.Append("ROT=" + this.ROT + ":");
            }

            return returnString.ToString();
        }
    }
}
